package com.gem.eolane.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gem.eolane.config.AppConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.gem.eolane.entity.PcbOffer;
import com.gem.eolane.entity.User;
import com.gem.eolane.service.PcbOfferService;
import com.gem.eolane.service.SurfaceService;
import com.gem.eolane.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gem.eolane.utils.Enande;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//前台拦截
@Controller
@RequestMapping("/f/u")
public class UReceInterfaceController {

    @Autowired
    private SurfaceService surfaceService;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    PcbOfferService pcbOfferService;

    @Autowired
    private UserService userService;


    //=====================徐jianghui========================

    //显示当前用户的历史订单
    @RequestMapping("/odform")
    public String odform(Model model, HttpSession session, Integer page, Integer size) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        if (page == null){
            page = 1;
        }
//        int count=surfaceService.fcount(id);
//        List<PcbOffer> list = surfaceService.findUid(id,page,size);
//        list.forEach(System.out::print);
        IPage<PcbOffer> iPage = surfaceService.findUid(id,page,size);
        model.addAttribute("list",iPage.getRecords());
        model.addAttribute("count",iPage.getTotal());
        model.addAttribute("curr",iPage.getCurrent());
        return "surfacepage/order_form";
    }
    //显示当前用户的历史订单详情
    @RequestMapping("/redetail")
    public String redetail(PcbOffer pcbOffer,Model model) {
        List<PcbOffer> list = surfaceService.findpcblist(pcbOffer.getId());
        PcbOffer pcbOffer1 = list.get(0);
        model.addAttribute("PcbOffer",pcbOffer1);
        return "surfacepage/PCB_Reveal";
    }

    //=====================徐jianghui========================

    //=====================袁zhihao==========================

    /*提交订单*/
    @RequestMapping("/pcborder")
    public String pcborder (HttpServletRequest httpServletRequest, PcbOffer pcbOffer) throws Exception{
        User user =  (User) httpServletRequest.getSession().getAttribute("user");
//        System.out.println("***************"+pcbOffer.getWidth());
//        System.out.println("***************"+pcbOffer.getLayerNumber());
//        System.out.println("***************"+pcbOffer.getUprice());
//        System.out.println("***************"+pcbOffer.getThickness());
//        System.out.println("***************"+pcbOffer.getMaterial());
        System.out.println("***************6"+pcbOffer.getCreateTime());
        pcbOfferService.saveOrder(pcbOffer,user);
        return "redirect:odform";
    }

    //=====================袁zhihao==========================


    //=====================刘hui=============================
    //个人中心
    @RequestMapping("/percenter")
    public ModelAndView percenter(HttpServletRequest request, User user, Model model) {
        HttpSession session = request.getSession();
        user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        /*        model.addAttribute("user",user);*/
        modelAndView.addObject("user", user);
        modelAndView.setViewName("surfacepage/percenter");
        return modelAndView;
    }

    //修改密码
    @RequestMapping("/updatepwd")
    public String updatepwd(HttpServletRequest request, User user, Model model) {
        HttpSession httpSession = request.getSession();
        user = (User) httpSession.getAttribute("user");
        model.addAttribute("userName", user.getUserName());
        return "surfacepage/updatepwd";
    }


    //基本信息修改
    @RequestMapping("updateUser")
    public String updateUser(User user, HttpSession session,Model model,HttpServletRequest request) {
        session =  request.getSession();
        User user2= (User) session.getAttribute("user");
        System.out.println(user2);
        user.setId(user2.getId());
        userService.findupdateUser(user);
        User user1 = userService.findQueryUser(user.getPhoneNumber());
        session.setAttribute("user", userService.findQueryUser(user.getPhoneNumber()));
        System.out.println("weqwrqrqwrq");
        boolean su = true;
        model.addAttribute("su",su);
        model.addAttribute("user",user1);
        return "surfacepage/percenter";
    }



    //密码修改
    @RequestMapping("updatepassword")
    public String updatepwd(HttpServletRequest request, User user, String passwordX, Model model, String password, String passwordX1) {
        HttpSession httpSession = request.getSession();
        Integer flag = 3;
        user = (User) httpSession.getAttribute("user");
        if (passwordX1.equals(passwordX)) {
            String pwd = Enande.decrypt(user.getPassword());

            if (pwd.equals(password)) {
                user.setPassword(Enande.encrypt(passwordX));

                userService.findUptadePwd(user);

                model.addAttribute("flag", flag);
                return "redirect:/f/t/login";
            } else {

                model.addAttribute("flag", flag);
                model.addAttribute("userName", user.getUserName());
                return "surfacepage/updatepwd";
            }
        }
        flag = 4;
        model.addAttribute("flag", flag);
        model.addAttribute("userName", user.getUserName());
        return "surfacepage/updatepwd";
    }
    @RequestMapping("/editor")
    @ResponseBody
    public Object editor(MultipartFile file) {
        String fileName ="";
        if(!file.isEmpty()){
            //返回的是字节长度,1M=1024k=1048576字节 也就是if(fileSize<5*1048576)
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

            fileName = System.currentTimeMillis()+suffix;
            String saveFileName = appConfig.getFilepath()+"/eolane/head/"+fileName;
            System.out.println("filePath" + saveFileName);
            File dest = new File(saveFileName);
            if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest); //保存文件
            } catch (Exception e) {
                e.printStackTrace();
                return new WangEditorResponse("1","上传失败"+e.getMessage());
            }
        }else {
            return new WangEditorResponse("1","上传出错");
        }
        String imgUrl=appConfig.getUrlpath()+"eolane/"+fileName;
        String img = fileName;
        List<String> data = new ArrayList<>();
        data.add(imgUrl);
        data.add(img);
        return new WangEditorResponse("0",data );
    }

    @Data
    private class WangEditorResponse{
        String errno;
        List<String> data;
        public WangEditorResponse(String errno,List<String> data){
            this.errno=errno;
            this.data=data;
        }
        public WangEditorResponse(String errno,String data){
            this.errno=errno;
            this.data=new ArrayList<>();
            this.data.add(data);
        }
    }

    //退出
    @RequestMapping("/withdraw")
    public String withdraw(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/f/t/showmain";
    }
    //=====================刘hui=============================
}
